import { readFileSync } from 'node:fs';
import { resolveModule } from 'local-pkg';
import type { PackageIndexes } from '@vueuse/metadata';
import type { ImportsMap } from 'unplugin-auto-import/types';

let _cache: ImportsMap | undefined;

export default (): ImportsMap => {
  if (!_cache) {
    let indexesJson: PackageIndexes | undefined;
    try {
      const corePath = resolveModule('@vueuse/core') || process.cwd();
      const path =
        resolveModule('@vueuse/metadata/index.json') ||
        resolveModule('@vueuse/metadata/index.json', { paths: [corePath] });
      indexesJson = JSON.parse(readFileSync(path!, 'utf-8'));
    } catch (error) {
      console.error(error);
      throw new Error(
        '[auto-import] failed to load @vueuse or desired addons, have you installed it?',
      );
    }
    if (indexesJson) {
      _cache = {};
      for (const addon of ['motion', 'sound', 'electron']) {
        _cache[`@vueuse/${addon}`] = indexesJson.functions
          .filter(i => [addon].includes(i.package))
          .flatMap(i => [i.name, ...(i.alias || [])])
          // only include functions with 4 characters or more
          .filter((i: string) => i && i.length >= 4);
      }
    }
  }

  return _cache || {};
};
